package team.pwy.service;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import team.pwy.consts.RbmConsts;
import team.pwy.mapper.SeckillMapper;
import team.pwy.pojo.entity.Seckill;

import java.util.*;

@Component
public class SeckillServiceImpl implements SeckillService {

    @Resource
    private SeckillMapper seckillMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<Seckill> findAll() {
        return seckillMapper.findAll();
    }

    @Override
    public boolean findBeforeActiveIds() {
        List<Seckill> seckillList = seckillMapper.findBeforeActiveIds();
        if (Objects.isNull(seckillList) && !seckillList.isEmpty()) {
            ListOperations lop = redisTemplate.opsForList();
            ValueOperations vop = redisTemplate.opsForValue();
            for (Seckill s : seckillList) {
                Integer num = s.getNumber();
                for (int i = 0; i < num; i++) {
                    lop.leftPush("sec_" + s.getId(), s.getId());
                }
                // 状态存储一下
                vop.set("sec_status_" + s.getId(), "0");
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean findSeckillActive() {
        List<Seckill> seckillList = seckillMapper.findSeckillActive();
        if (Objects.nonNull(seckillList) && !seckillList.isEmpty()) {
            ListOperations lop = redisTemplate.opsForList();
            ValueOperations vop = redisTemplate.opsForValue();
            for (Seckill s : seckillList) {
                Integer num = s.getNumber();
                for (int i = 0; i < num; i++) {
                    lop.leftPush("sec_" + s.getId(), s.getId());
                }
                // 状态存储一下
                vop.set("sec_status_" + s.getId(), "1");
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateActiveStatus() {
        seckillMapper.updateActiveStatus();
        List<Integer> ids = seckillMapper.findActiveIds();
        // 更新redis中的状态
        if (Objects.nonNull(ids) && !ids.isEmpty()) {
            ValueOperations vop = redisTemplate.opsForValue();
            for (Integer id : ids) {
                vop.set("sec_status_" + id, "1");
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateEndStatus() {
        seckillMapper.updateEndStatus();
        List<Integer> ids = seckillMapper.findAfterActiveIds();
        // 更新redis中的状态
        if (Objects.nonNull(ids) && !ids.isEmpty()) {
            ValueOperations vop = redisTemplate.opsForValue();
            for (Integer id : ids) {
                vop.set("sec_status_" + id, "2");
            }
            return true;
        }
        return false;
    }

    @Override
    public List<Integer> findAfterActiveIds() {
        return seckillMapper.findAfterActiveIds();
    }

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public String getStatus(Integer secId) {
        String status = (String) redisTemplate.opsForValue().get("sec_status_" + secId);
        if (null == status) {
            return "0";
        }
        return status;
    }

    // 投递秒杀结果到rabbit
    @Override
    public void sendRbm(Integer userId, Integer secId) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("userId", userId);
        maps.put("secId", secId);
        rabbitTemplate.convertAndSend(RbmConsts.SEC_QUEUE, maps);
    }
}
